package cn.iocoder.yudao.module.biz.service.operationTicket;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.operationTicket.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.operationTicket.BizOperationTicketDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.operationTicket.BizOperationTicketConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.operationTicket.BizOperationTicketMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 操作票 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizOperationTicketServiceImpl implements BizOperationTicketService {

    @Resource
    private BizOperationTicketMapper operationTicketMapper;

    @Override
    public String createOperationTicket(BizOperationTicketCreateReqVO createReqVO) {
        // 插入
        BizOperationTicketDO operationTicket = BizOperationTicketConvert.INSTANCE.convert(createReqVO);
        operationTicketMapper.insert(operationTicket);
        // 返回
        return operationTicket.getId();
    }

    @Override
    public void updateOperationTicket(BizOperationTicketUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateOperationTicketExists(updateReqVO.getId());
        // 更新
        BizOperationTicketDO updateObj = BizOperationTicketConvert.INSTANCE.convert(updateReqVO);
        operationTicketMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperationTicket(String id) {
        // 校验存在
        this.validateOperationTicketExists(id);
        // 删除
        operationTicketMapper.deleteById(id);
    }

    private void validateOperationTicketExists(String id) {
        if (operationTicketMapper.selectById(id) == null) {
            throw exception(OPERATION_TICKET_NOT_EXISTS);
        }
    }

    @Override
    public BizOperationTicketDO getOperationTicket(String id) {
        return operationTicketMapper.selectById(id);
    }

    @Override
    public List<BizOperationTicketDO> getOperationTicketList(Collection<String> ids) {
        return operationTicketMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizOperationTicketDO> getOperationTicketPage(BizOperationTicketPageReqVO pageReqVO) {
        return operationTicketMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizOperationTicketDO> getOperationTicketList(BizOperationTicketExportReqVO exportReqVO) {
        return operationTicketMapper.selectList(exportReqVO);
    }

}
